package com.quan.weixin.web.controller;

import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.annotation.Log;
import com.quan.common.bean.R;
import com.quan.common.core.MyBaseController;
import com.quan.common.enums.LogType;
import com.quan.system.entity.SystemSettings;
import com.quan.weixin.entity.WeixinSubscribe;
import com.quan.weixin.service.WeixinSubscribeService;

/**
 * <p>
 * 微信用户关注 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-09-06
 */
@RestController
@RequestMapping("/weixin/subscribe")
public class WeixinSubscribeController extends MyBaseController {

	@Autowired 
    private WeixinSubscribeService service;
    
    /**
	 * 添加
	 * 
	 * @param entity
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/add") @Log(type=LogType.BUSINESS, desc = "添加")
	public Object add(@Valid @RequestBody WeixinSubscribe entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return R.warning(bindingResult.getFieldError().getRejectedValue().toString());
		}
		boolean save = this.service.save(entity);
		if (save) {
			return R.ok("提交成功");
		} else {
			return R.failure("提交失败");
		}
	}
	
	/**
	 * 添加或编辑
	 * @param entity
	 * @return
	 */
	@PostMapping("/saveOrUpdate")
	public Object saveOrUpdate(@RequestBody WeixinSubscribe entity) {
		boolean save = this.service.saveOrUpdate(entity) ;
		if (save) {
			return R.ok(entity.getId());
		} else {
			return R.failure("提交失败");
		}
	}

	/**
	 * 根据ID删除
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/deleteById/{id}") @Log(type=LogType.BUSINESS, desc = "根据ID删除")
	public Object deleteById(@PathVariable String id) {
		if (StringUtils.isNotBlank(id)) {
			boolean remove = this.service.removeById(id);
			if (remove) {
				return R.ok();
			} else {
				return R.failure("通过ID删除失败：[ " + id + " ]");
			}
		}
		return R.failure("请选项需要删除的记录");
	}

	/**
	 * 根据ID批量删除
	 * 
	 * @param idList ["1","2"]
	 * @return
	 */
	@PostMapping("/deleteBatchIds") @Log(type=LogType.BUSINESS, desc = "根据ID批量删除")
	public Object deleteBatchIds(@RequestBody List<String> idList) {
		if (null != idList && !idList.isEmpty()) {
			boolean remove = this.service.removeByIds(idList);
			if (remove) {
				return R.ok();
			} else {
				return R.failure("通过ID删除失败：[ " + idList + " ]");
			}
		}
		return R.failure("请选项需要删除的记录");
	}

	/**
	 * 修改
	 * 
	 * @param entity
	 * @param bindingResult
	 * @return
	 */
	@PostMapping("/update") @Log(type=LogType.BUSINESS, desc = "修改")
	public Object update(@Valid @RequestBody WeixinSubscribe entity, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return R.failure(bindingResult.getFieldError().getDefaultMessage().toString());
		}
		boolean update = this.service.updateById(entity);
		if (update) {
			return R.ok("修改成功");
		} else {
			return R.failure("修改失败");
		}
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{id}") @Log(type=LogType.BUSINESS, desc = "根据ID查询")
	public Object get(@PathVariable Long id) {
		WeixinSubscribe entity = this.service.getById(id);
		if (null != entity) {
			return R.ok(entity);
		} else {
			return R.failure("通过ID查询失败：[ " + id + " ]");
		}
	}

	/**
	 * 查询所有数据，不分页
	 * 只查询第一条
	 * @param entity
	 * @return
	 */
	@GetMapping(value = "/list")
	public Object list(SystemSettings entity) {
		return R.ok(this.service.loadSubscribe());
	}

}



